const electron = require('electron');
const Store = require('electron-store');
const qqMusic = require('qq-music-api');
const store = new Store();
const ipcMain = electron.ipcMain;
const head = 'qq/';
module.exports = class qq {
    cookie = {};
    constructor() {
        this.retryGetCookie();
    }
    retryGetCookie(){
        //console.log(this.cookie)
        if(this.cookie !== {} && this.cookie !== undefined && this.cookie.uid !== undefined){
            return
        }
        let cookie_get = store.get('qqcookie');
        if(cookie_get !== undefined && cookie_get !== ''){
            let coo = {};
            cookie_get.split(';').map((r) => {

                let kv = r.split('=');
                let k = kv[0].trim();
                let v = kv[1]? kv[1] : "";
                coo[k] = v.trim();
            });
            this.cookie = coo;
        }
    }
    init() {
        
        ipcMain.handle(head+'user/setCookie', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('user/setCookie', { ...args},this.cookie)
        })
        ipcMain.handle(head+'recommend/playlist/u', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('recommend/playlist/u', { ...args},this.cookie)
        })
        ipcMain.handle(head+'songlist', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('songlist', { ...args},this.cookie)
        })
        ipcMain.handle(head+'song/urls', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('song/urls', { ...args},this.cookie)
        })
        ipcMain.handle(head+'album', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('album', { ...args},this.cookie)
        })
        ipcMain.handle(head+'user/detail', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('user/detail', { ...args},this.cookie)
        })
        ipcMain.handle(head+'search', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('search', { ...args},this.cookie)
        })
        ipcMain.handle(head+'singer/album', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('singer/album', { ...args},this.cookie)
        })
        ipcMain.handle(head+'album/songs', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('album/songs', { ...args},this.cookie)
        })
        ipcMain.handle(head+'songlist/category', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('songlist/category', { ...args},this.cookie)
        })
        ipcMain.handle(head+'songlist/list', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('songlist/list', { ...args},this.cookie)
        })
        ipcMain.handle(head+'top/category', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('top/category', { ...args},this.cookie)
        })
        ipcMain.handle(head+'top', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('top', { ...args},this.cookie)
        })
        ipcMain.handle(head+'singer/category', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('singer/category', { ...args},this.cookie)
        })
        ipcMain.handle(head+'singer/list', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('singer/list', { ...args},this.cookie)
        })
        ipcMain.handle(head+'lyric', async (event, args) => {
            this.retryGetCookie();
            return await qqMusic.api('lyric', { ...args},this.cookie)
        })


    }

}
